package com.guoliang.statisticsService.service.impl;

import com.alibaba.nacos.common.utils.RandomUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guoliang.commonUtils.CommonResult;
import com.guoliang.statisticsService.entity.StatisticsDaily;
import com.guoliang.statisticsService.mapper.StatisticsDailyMapper;
import com.guoliang.statisticsService.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guoliang.statisticsService.service.UcenterClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author leo
 * @since 2023-02-01
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public void createStaByDay(String day) {
        //先删除day相同的数据
        QueryWrapper<StatisticsDaily> dailyQueryWrapper=new QueryWrapper<>();
        dailyQueryWrapper.eq("date_calculated",day);
        baseMapper.delete(dailyQueryWrapper);
        //获取统计信息
        CommonResult registerResult = ucenterClient.getRegisterNum(day);
        Integer registerNum=(Integer) registerResult.getData().get("registerNum");
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO
        //创建统计对象
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(registerNum);
        statisticsDaily.setLoginNum(loginNum);
        statisticsDaily.setVideoViewNum(videoViewNum);
        statisticsDaily.setCourseNum(courseNum);
        statisticsDaily.setDateCalculated(day);
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getStatistics(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select(type,"date_calculated");
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);
        List<String> days=new ArrayList<>();
        List<Integer> nums=new ArrayList<>();

        for (StatisticsDaily daily : dailyList) {
          days.add(daily.getDateCalculated());
          switch (type){
              case "register_num":
                  nums.add(daily.getRegisterNum());
                  break;
              case "login_num":
                  nums.add(daily.getLoginNum());
                  break;
              case "video_view_num":
                  nums.add(daily.getVideoViewNum());
                  break;
              case "course_num":
                  nums.add(daily.getCourseNum());
                  break;
          }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("date",days);
        map.put("nums",nums);
        return map;
    }
}
